import time
import unittest

from ddt import ddt, data, unpack
from selenium import webdriver
from selenium.webdriver.common.by import By
# from unittest import TestCase
from method import method
from _init_ import TestCaseOne
'''
da = [
    ['lisi', 123456],
    ['admin', 123456],
    ['lisi', 123456, 'lisi3'],
    ['lisi', 1234567],
    ['admin', 123456, 'admin1'],
    ['admin', 1234567]
]
'''

da1 = [
    ['lisi', 123456]
]
da2 = [
    ['admin', 123456]
]
da3 = [
    ['lisi', 123456, 'lisi3']
]
da4 = [
    ['lisi', 1234567]
]
da5 = [
    ['admin', 123456, 'admin1']
]
da6 = [
    ['admin', 1234567]
]


# 选择跳过的方法
def chooseSkip(get_skip_or_no_skip, get_choose_case):
    all_test = []
    for i in range(1, 7):
        all_test.append(i)
    for j in get_choose_case:
        all_test.remove(j)
    if get_skip_or_no_skip == 1:
        the_choose_skip = all_test
    else:
        the_choose_skip = get_choose_case
    return the_choose_skip


# 选择跳过这些用例则选择0，选择不跳过这些用例则选择1
skip_or_no_skip = 0
choose_case = []
choose_skip = chooseSkip(skip_or_no_skip, choose_case)
s = method()
# 创建一个列表用来存储选择，有多少方法就准备多少元素
skip_test = s.skipTest(6, choose_skip)


@ddt
class TestLogin(TestCaseOne):
    @unittest.skipIf(skip_test[0] == 1, "暂时跳过")
    @data(*da1)
    @unpack
    def test_001_LoginSuccessUser(self, a, b):
        # 清理数据库，准备测试数据
        t = s.findUsername(a)
        if len(t) != 0:
            s.cleanDatabaseByUsername(a)
            s.addUsername(a)
        else:
            s.addUsername(a)
        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常做登陆测试
        ele = driver.find_element(By.XPATH, "//input[@id='username']")
        ele.send_keys(a)
        time.sleep(1)

        #  输入密码
        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys(b)
        time.sleep(1)

        # 点击登陆
        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()
        time.sleep(3)

        #  获取实际标题结果
        result = driver.find_element(By.XPATH, "//div[@id='logo']/a").text

        if result == "个人理财系统":
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_001_LoginSuccessUser")
            driver.save_screenshot(screenshot)

        time.sleep(3)

        self.assertEqual(result, "个人理财系统")

        driver.close()

    @unittest.skipIf(skip_test[1] == 1, "暂时跳过")
    @data(*da2)
    @unpack
    def test_002_LoginSuccessAdmin(self, a, b):
        # 清理数据库，准备测试数据
        t = s.findAdmin(a)
        if len(t) != 0:
            s.cleanAdminByUsername(a)
            s.addAdmin(a)
        else:
            s.addAdmin(a)

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常做登陆测试
        ele = driver.find_element(By.XPATH, "//input[@id='username']")
        ele.send_keys(a)

        time.sleep(1)
        #  输入密码
        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys(b)

        time.sleep(1)

        # 点击登陆
        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()
        time.sleep(3)

        #  获取实际标题结果
        result = driver.find_element(By.XPATH, "//div[@id='logo']/a").text

        if result == "后台管理系统":
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_002_LoginSuccessAdmin")
            driver.save_screenshot(screenshot)

        time.sleep(3)

        self.assertEqual(result, "后台管理系统")

        driver.close()

    @unittest.skipIf(skip_test[2] == 1, "暂时跳过")
    @data(*da3)
    @unpack
    def test_003_WrongUserName(self, a, b, c):
        # 清理数据库，准备测试数据
        t = s.findUsername(a)
        if len(t) != 0:
            s.cleanDatabaseByUsername(a)
            s.addUsername(a)
        else:
            s.addUsername(a)

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常做登陆测试
        ele = driver.find_element(By.XPATH, "//input[@id='username']")
        ele.send_keys(c)

        time.sleep(1)
        #  输入密码
        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys(b)

        time.sleep(1)

        # 点击登陆
        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()
        time.sleep(3)

        #  获取实际标题结果
        resultall = driver.find_elements(By.CLASS_NAME, "help-block")
        result = resultall[0].text
        if result == "用户名不存在":
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_003_WrongUserName")
            driver.save_screenshot(screenshot)

        time.sleep(3)

        self.assertEqual(result, "用户名不存在")

        driver.close()

    @unittest.skipIf(skip_test[3] == 1, "暂时跳过")
    @data(*da4)
    @unpack
    def test_004_WrongPassword(self, a, b):
        # 清理数据库，准备测试数据
        t = s.findUsername(a)
        if len(t) != 0:
            s.cleanDatabaseByUsername(a)
            s.addUsername(a)
        else:
            s.addUsername(a)

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常做登陆测试
        ele = driver.find_element(By.XPATH, "//input[@id='username']")
        ele.send_keys(a)

        time.sleep(1)
        #  输入密码
        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys(b)

        time.sleep(1)

        # 点击登陆
        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()
        time.sleep(3)

        #  获取实际标题结果
        resultall = driver.find_elements(By.CLASS_NAME, "help-block")
        result = resultall[1].text
        if result == "密码错误":
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_004_WrongPassword")
            driver.save_screenshot(screenshot)

        time.sleep(3)

        self.assertEqual(result, "密码错误")

        driver.close()

    @unittest.skipIf(skip_test[4] == 1, "暂时跳过")
    @data(*da5)
    @unpack
    def test_005_WrongUsernameAdmin(self, a, b, c):
        # 清理数据库，准备测试数据
        t = s.findAdmin(a)
        if len(t) != 0:
            s.cleanAdminByUsername(a)
            s.addAdmin(a)
        else:
            s.addAdmin(a)

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常做登陆测试
        ele = driver.find_element(By.XPATH, "//input[@id='username']")
        ele.send_keys(c)

        time.sleep(1)
        #  输入密码
        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys(b)

        time.sleep(1)

        # 点击登陆
        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()
        time.sleep(3)

        #  获取实际标题结果
        resultall = driver.find_elements(By.CLASS_NAME, "help-block")
        result = resultall[0].text
        if result == "用户名不存在":
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_005_WrongUsernameAdmin")
            driver.save_screenshot(screenshot)

        time.sleep(3)

        self.assertEqual(result, "用户名不存在")

        driver.close()

    @unittest.skipIf(skip_test[5] == 1, "暂时跳过")
    @data(*da6)
    @unpack
    def test_006_WrongPasswordAdmin(self, a, b):
        # 清理数据库，准备测试数据
        t = s.findAdmin(a)
        if len(t) != 0:
            s.cleanAdminByUsername(a)
            s.addAdmin(a)
        else:
            s.addAdmin(a)

        driver = webdriver.Chrome()

        driver.get("http://localhost:90/")

        driver.maximize_window()

        # 正常做登陆测试
        ele = driver.find_element(By.XPATH, "//input[@id='username']")
        ele.send_keys(a)

        time.sleep(1)
        #  输入密码
        ele2 = driver.find_element(By.XPATH, "//input[@id='password']")
        ele2.send_keys(b)

        time.sleep(1)

        # 点击登陆
        ele3 = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        ele3.click()
        time.sleep(3)

        #  获取实际标题结果
        resultall = driver.find_elements(By.CLASS_NAME, "help-block")
        result = resultall[1].text
        if result == "密码错误":
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_006_WrongPasswordAdmin")
            driver.save_screenshot(screenshot)

        time.sleep(3)

        self.assertEqual(result, "密码错误")

        driver.close()